package edu.buaa.act.helper;

import edu.buaa.act.helper.Arguments.Param;
import edu.buaa.act.helper.Arguments.Values;

import java.util.*;

/**
 * Created by T7810 on 2017/5/27.
 */
public class Config
{
    private static int nextId = 0;

    private Map<Values, Param> properties = new HashMap<>();

    public <T> T get(Values name){
        return properties.get(name).get();
    }

    public void set(Values name, Object val){
        properties.put(name, new Param(name, val));
    }

    public String toString(boolean oneLine){
        List<Values> values = new ArrayList<>();
        for(Map.Entry<Values, Param> pair : this.properties.entrySet()){
            values.add(pair.getKey());
        }
        values.sort((o1, o2) -> Integer.valueOf(o1.id).compareTo(o2.id));

        StringBuilder sb = new StringBuilder();
        if(oneLine){
            StringBuilder vals = new StringBuilder();
            for(Values v : values){
                sb.append(v).append(' ');
                vals.append( properties.get(v).getStr() ).append(' ');
            }
            return sb.toString()+vals.toString();
        }else{
            for(Values v : values){
                sb.append(v).append(' ').append(properties.get(v).getStr()).append('\n');
            }
            return sb.toString();
        }
    }

    public String toString(Values ... values) {
        StringBuilder sb = new StringBuilder();
        for(Values value : values){
            Param p = this.properties.get(value);
            Object v = p.get();
            sb.append(value).append(' ');
            if(v instanceof double[]){
                sb.append(Arrays.toString((double[]) v)).append('\n');
            }else {
                sb.append(p.getStr()).append('\n');
            }
        }
        return sb.toString();
    }

    public String toTableString(Values ... values) {
        StringBuilder sb = new StringBuilder();
        sb.append("#####################################\n# ");
        for(Values value : values)
        {
            sb.append(value).append(", ");
        }
        sb.append("\n#----------------------------------\n# ");
        for(Values value : values)
        {
            Param p = this.properties.get(value);
            sb.append(p.getStr()).append(", ");
        }
        sb.append("\n#######################################\n");
        return sb.toString();
    }

    public String tableHeader(Values ... values)
    {
        StringBuilder sb = new StringBuilder();
        for(Values value : values)
        {
            sb.append(value).append(", ");
        }
        return sb.substring(0, sb.length()-2);
    }

    public String tableRow(Values ... values)
    {
        boolean hasContent = false;
        StringBuilder sb = new StringBuilder();
        for(Values value : values)
        {
            Param p = this.properties.get(value);
            if(p!=null)
            {
                hasContent=true;
                sb.append(p.getStr()).append(", ");
            }
        }
        if(hasContent)
        {
            return sb.substring(0, sb.length() - 2);
        }else{
            return "";
        }
    }

    public static int nextId() {
        int t = nextId;
        nextId++;
        return t;
    }
}
